home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BPUTBF.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  169 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bputbf.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* local headers */
  18. #include "blkio_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      bputbf - put a block field into a block file
  23.  
  24. SYNOPSIS
  25.      #include <blkio.h>
  26.  
  27.      int bputbf(bp, bn, offset, buf, bufsize)
  28.      BLKFILE *bp;
  29.      bpos_t bn;
  30.      size_t offset;
  31.      const void *buf;
  32.      size_t bufsize;
  33.  
  34. DESCRIPTION
  35.      The bputbf function writes the contents of buf into a field of
  36.      block number bn of the block file associated with BLKFILE pointer
  37.      bp.  The field begins offset characters from the beginning of the
  38.      block and is bufsize characters long.  buf must point to a
  39.      storage area at least bufsize characters long.  Block numbering
  40.      starts at 1.
  41.  
  42.      bputbf will fail if one or more of the following is true:
  43.  
  44.      [EINVAL]       bp is not a valid BLKFILE pointer.
  45.      [EINVAL]       bn or bufsize is less than 1.
  46.      [EINVAL]       buf is the NULL pointer.
  47.      [BEBOUND]      offset + bufsize extends beyond the
  48.                     boundary of block bn.
  49.      [BEEOF]        Partial block being written and block
  50.                     bn is past the end of file.
  51.      [BEEOF]        Complete block being written and block
  52.                     bn is more than 1 past the end of file.
  53.      [BENOPEN]      bp is not open for writing.
  54.  
  55. SEE ALSO
  56.      bgetbf, bputb, bputhf.
  57.  
  58. DIAGNOSTICS
  59.      Upon successful completion, a value of 0 is returned.  Otherwise,
  60.      a value of -1 is returned, and errno set to indicate the error.
  61.  
  62. NOTES
  63.      Whenever a new block is created at the end of the file, the
  64.      entire block must be written, i.e., offset must have a value of 0
  65.      and bufsize must be equal to the block size.
  66.  
  67. ------------------------------------------------------------------------------*/
  68. #ifdef AC_PROTO
  69. int bputbf(BLKFILE *bp, bpos_t bn, size_t offset, const void *buf, size_t bufsize)
  70. #else
  71. int bputbf(bp, bn, offset, buf, bufsize)
  72. BLKFILE *bp;
  73. bpos_t bn;
  74. size_t offset;
  75. const void *buf;
  76. size_t bufsize;
  77. #endif
  78. {
  79.     int    i    = 0;
  80.     size_t    bufno    = 0;
  81.  
  82.     /* validate arguments */
  83.     if (!b_valid(bp) || bn < 1 || buf == NULL || bufsize < 1) {
  84.         errno = EINVAL;
  85.         return -1;
  86.     }
  87.  
  88.     /* check if not open for writing */
  89.     if (!(bp->flags & BIOWRITE)) {
  90.         errno = BENOPEN;
  91.         return -1;
  92.     }
  93.  
  94.     /* check if block boundary is crossed */
  95.     if ((offset + bufsize) > bp->blksize) {
  96.         errno = BEBOUND;
  97.         return -1;
  98.     }
  99.  
  100.     /* check if past end of file */
  101.     if (offset != 0 || bufsize != bp->blksize) {
  102.         if (bn >= bp->endblk) {
  103.             errno = BEEOF;
  104.             return -1;
  105.         }
  106.     } else {
  107.         if (bn > bp->endblk) {
  108.             errno = BEEOF;
  109.             return -1;
  110.         }
  111.     }
  112.  
  113.     /* check if not buffered */
  114.     if (bp->bufcnt == 0) {
  115.         if (b_uputf(bp, bn, offset, buf, bufsize) == -1) {
  116.             BEPRINT;
  117.             return -1;
  118.         }
  119.         if (bp->endblk <= bn) {
  120.             bp->endblk = bn + 1;
  121.         }
  122.         return 0;
  123.     }
  124.  
  125.     /* search buffer list for block */
  126.     for (i = 1; i <= bp->bufcnt; ++i) {
  127.         if ((b_blockp(bp, (size_t)i)->bn == bn)
  128.                    && (b_blockp(bp, (size_t)i)->flags & BLKREAD)) {
  129.             bufno = i;
  130.             break;
  131.         }
  132.     }
  133.  
  134.     /* if not found, use least recently used buffer */
  135.     if (bufno == 0) {
  136.         bufno = bp->least;
  137.         if (b_put(bp, bufno) == -1) {    /* flush previous contents */
  138.             BEPRINT;
  139.             return -1;
  140.         }
  141.         b_blockp(bp, bufno)->flags = 0;
  142.         b_blockp(bp, bufno)->bn = bn;
  143.         if (offset != 0 || bufsize != bp->blksize) {
  144.             /* read block from file */
  145.             if (b_get(bp, bufno) == -1) {
  146.                 if (errno != BEEOF) BEPRINT;
  147.                 return -1;
  148.             }
  149.         }
  150.     }
  151.  
  152.     /* copy from buf into block buffer and set flags */
  153.     memcpy(((char *)b_blkbuf(bp, bufno) + offset), buf, bufsize);
  154.     b_blockp(bp, bufno)->flags = BLKREAD | BLKWRITE;
  155.  
  156.     /* adjust endblk */
  157.     if (bp->endblk <= bn) {
  158.         bp->endblk = bn + 1;
  159.     }
  160.  
  161.     /* move block buffer bufno to most recently used end of list */
  162.     if (b_mkmru(bp, bufno) == -1) {
  163.         BEPRINT;
  164.         return -1;
  165.     }
  166.  
  167.     return 0;
  168. }
  169.